package lu.med.praxiswahl.wahlumed.client.analysenintern;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import lu.med.praxiswahl.wahlumed.client.wclient.Main;


@SuppressWarnings("serial")
public class AnalysenIntern2ndComburDialog extends JDialog implements
	ActionListener
{

    private AnalysenInternDataModel model;
    private int row;
    private JRadioButton bb1, bb2;
    private JRadioButton ob, pos;
    private JTextField[] tf = new JTextField[10];

    public AnalysenIntern2ndComburDialog(AnalysenInternDataModel model,
	    int row, Main dataManager)
    {
	super(dataManager, "Combur Daten Eingabe", true);
	this.setLayout(new BorderLayout());
	this.model = model;
	this.row = row;

	JPanel buttonPanel, checkPanel, mainPanel;

	buttonPanel = new JPanel();
	JButton b1, b2;
	b1 = new JButton("Ok");
	b2 = new JButton("Abbrechen");
	b1.addActionListener(this);
	b2.addActionListener(this);
	buttonPanel.add(b1);
	buttonPanel.add(b2);

	checkPanel = new JPanel(new GridLayout(1, 0));
	checkPanel.setBorder(BorderFactory.createTitledBorder("Kategorie"));
	ButtonGroup bg = new ButtonGroup();
	bb1 = new JRadioButton("Urin");
	bb2 = new JRadioButton("Mittelstrahl Urin");
	bg.add(bb1);
	bg.add(bb2);
	ButtonGroup bg2 = new ButtonGroup();
	ob = new JRadioButton("o.B.");
	pos = new JRadioButton("POS");
	bg2.add(ob);
	bg2.add(pos);
	checkPanel.add(bb1);
	checkPanel.add(bb2);
	checkPanel.add(ob);
	checkPanel.add(pos);

	JPanel grid = new JPanel(new GridLayout(0, 2));
	for (int i = 0; i < 10; i++)
	{
	    switch (i)
	    {
	    case 0:
		grid.add(new JLabel("Leukocyten:"));
		break;
	    case 1:
		grid.add(new JLabel("Nitrit:"));
		break;
	    case 2:
		grid.add(new JLabel("pH:"));
		break;
	    case 3:
		grid.add(new JLabel("Protein:"));
		break;
	    case 4:
		grid.add(new JLabel("Glucose:"));
		break;
	    case 5:
		grid.add(new JLabel("Keton:"));
		break;
	    case 6:
		grid.add(new JLabel("Urobilonogen:"));
		break;
	    case 7:
		grid.add(new JLabel("Bilirubin:"));
		break;
	    case 8:
		grid.add(new JLabel("Blut:"));
		break;
	    case 9:
		grid.add(new JLabel("Haemoglobin:"));
		break;
	    }
	    tf[i] = new JTextField("");
	    grid.add(tf[i]);
	}

	mainPanel = new JPanel(new BorderLayout());
	mainPanel.setBorder(BorderFactory
		.createBevelBorder(BevelBorder.LOWERED));
	mainPanel.add(grid, BorderLayout.NORTH);

	// Überträgt die Daten aus dem Modell in die Eingabefelder
	tf[0].setText(model.getRow(row).getLeukocyten2nd());
	tf[1].setText(model.getRow(row).getNitrit2nd());
	tf[2].setText(model.getRow(row).getPH2nd());
	tf[3].setText(model.getRow(row).getProtein2nd());
	tf[4].setText(model.getRow(row).getGlucose2nd());
	tf[5].setText(model.getRow(row).getKeton2nd());
	tf[6].setText(model.getRow(row).getUrobiligonen2nd());
	tf[7].setText(model.getRow(row).getBilirubin2nd());
	tf[8].setText(model.getRow(row).getBlut2nd());
	tf[9].setText(model.getRow(row).getHaemoglobin2nd());
	if (model.getRow(row).isCheckMittelstrahl2nd())
	{
	    bb2.setSelected(true);
	}
	else
	{
	    bb1.setSelected(true);
	}
	if (!model.getRow(row).isOb_pos2nd())
	{
	    ob.setSelected(true);
	}
	else
	    pos.setSelected(true);

	this.add(buttonPanel, BorderLayout.SOUTH);
	this.add(checkPanel, BorderLayout.NORTH);
	this.add(mainPanel, BorderLayout.CENTER);

	this.pack();
	this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
	if (e.getActionCommand().equals("Ok"))
	{
	    // Übernimmt die Daten ins Model
	    model.getRow(row).setLeukocyten2nd(tf[0].getText());
	    model.getRow(row).setNitrit2nd(tf[1].getText());
	    model.getRow(row).setPH2nd(tf[2].getText());
	    model.getRow(row).setProtein2nd(tf[3].getText());
	    model.getRow(row).setGlucose2nd(tf[4].getText());
	    model.getRow(row).setKeton2nd(tf[5].getText());
	    model.getRow(row).setUrobiligonen2nd(tf[6].getText());
	    model.getRow(row).setBilirubin2nd(tf[7].getText());
	    model.getRow(row).setBlut2nd(tf[8].getText());
	    model.getRow(row).setHaemoglobin2nd(tf[9].getText());
	    if (bb1.isSelected())
	    {
		model.getRow(row).setCheckMittelstrahl2nd(false);
		model.getRow(row).setCheckUrin2nd(true);
	    }
	    else
	    {
		model.getRow(row).setCheckMittelstrahl2nd(true);
		model.getRow(row).setCheckUrin2nd(false);
	    }
	    if (ob.isSelected())
	    {
		model.getRow(row).setOb_pos2nd(false);
	    }
	    else
	    {
		model.getRow(row).setOb_pos2nd(true);
	    }
	    this.setVisible(false);
	}
	else
	{
	    this.setVisible(false);
	}
    }
}
